package edu.tgc.web.controller.hrm;


import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.abyss.common.web.controller.easyui.CommonCrudEasyUiController;
import org.abyss.common.web.controller.easyui.EasyuiAjaxFormResult;
import org.abyss.common.web.controller.easyui.EasyuiResultBuilder;
import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpRequest;
import org.springframework.security.web.context.HttpRequestResponseHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.tgc.entity.hrm.IdCade;
import edu.tgc.service.hrm.IdCadeService;
import edu.tgc.web.form.hrm.IdCadeForm;
@Controller
@RequestMapping(value="/hrm/idCade")
public class IdCadeController extends CommonCrudEasyUiController<IdCade, UUID, IdCadeForm> {
	
	@Autowired
	private IdCadeService service;
	
	@RequestMapping(value="/noUsedPage")
	@ResponseBody
	public Object noUsedPage(Pageable pageable){
		Page<IdCade> page=service.noUserPage(pageable);
		return EasyuiResultBuilder.buildDataGridResult(page);
	}
	
	@RequestMapping(value="/save",method=RequestMethod.POST)
	@ResponseBody
	public Object save(@RequestParam(value="photo",required=false)MultipartFile file,IdCadeForm form,BindingResult bindingResult){
		try {
			service.save(file,form);
			return EasyuiResultBuilder.buildAjaxResult("身份证信息保存成功！");
		} catch (Exception e) {
			return EasyuiResultBuilder.buildAjaxResultError("身份证信息提交失败！");
		}
		
	}
	
	@RequestMapping(value="/imageStream",method=RequestMethod.GET)
	@ResponseBody
	public void imageStream(String idNo,HttpServletRequest request,HttpServletResponse response) throws IOException{
		service.imageStream(idNo,request,response);
	}

	@Override
	public EasyuiAjaxFormResult delete(IdCadeForm form, BindingResult bindingResult, Model model,
			RedirectAttributes redirectAttributes) {
		IdCade idCade=baseService.getById(form.getId());
		if(idCade.getPhoto()!=null){
			File file=new File(IdCade.getPath(), idCade.getIdNo());
			if(file.exists()){
				file.delete();
			}
		}
		return super.delete(form, bindingResult, model, redirectAttributes);
	}

	@Override
	public Specification<IdCade> buildSearch(IdCadeForm form) {
		HashSet<Rule> rules=new HashSet<>();
		Boolean isUsed=form.getIsUsed();
		if(isUsed!=null){
			rules.add(Rule.property("isUsed").equal(false));
		}
		String searchKey=form.getSearchKey();
		if(StringUtils.hasText(searchKey)){
			rules.add(Rule.or(Rule.property("name").contain(searchKey), Rule.property("idNo").contain(searchKey)));
		}
		UUID id=form.getId();
		if(id!=null){
			return Rule.or(Rule.property("id").equal(id),Rule.and(rules)).toSpecification();
		}else{
			return Rule.and(rules).toSpecification();
		}
	}
	
	
}
